﻿package com.yy.ui.controls {
	import com.yy.ui.core.UIComponent;

    public class SliderBar extends UIComponent {

        private var _sliderBg:DisplayObject;
        private var _sliderSkin:DisplayObject;
        private var _sliderBtn:DisplayObject;
        private var _max:int;
        private var _min:int;
        private var _currentValue:Number;
        private var rect:Rectangle;
        public var oldNum:Number = 0;

        public function SliderBar(_arg1:DisplayObject, _arg2:DisplayObject, _arg3:DisplayObject){
            this._sliderBg = _arg1;
            this._sliderSkin = _arg2;
            this._sliderBtn = _arg3;
            this.initView();
        }
        private function initView():void{
            addChild(this._sliderBg);
            addChild(this._sliderSkin);
            addChild(this._sliderBtn);
            this._sliderSkin.y = ((this._sliderBg.height - this._sliderSkin.height) / 2);
            this._sliderSkin.x = 2.5;
            this.updateUI();
            this.initListener();
        }
        private function initListener():void{
            Sprite(this._sliderBtn).buttonMode = true;
            this._sliderBtn.addEventListener(MouseEvent.MOUSE_DOWN, this.sliderMouseDown);
            this._sliderBtn.addEventListener(MouseEvent.MOUSE_UP, this.sliderMouseUp);
            Sprite(this._sliderBg).buttonMode = true;
            Sprite(this._sliderSkin).buttonMode = true;
            this._sliderBg.addEventListener(MouseEvent.MOUSE_DOWN, this.sliderSkinMouseDown);
            this._sliderSkin.addEventListener(MouseEvent.MOUSE_DOWN, this.sliderSkinMouseDown);
        }
        private function sliderMouseDown(_arg1:MouseEvent):void{
            this.rect = new Rectangle(0, this._sliderBtn.y, this._sliderBg.width, 0);
            this._sliderBtn.stage.addEventListener(MouseEvent.MOUSE_UP, this.sliderMouseUp);
            this._sliderBtn.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.sliderMouseMove);
            Sprite(this._sliderBtn).startDrag(false, this.rect);
        }
        private function sliderMouseUp(_arg1:MouseEvent):void{
            this._sliderBtn.stage.removeEventListener(MouseEvent.MOUSE_UP, this.sliderMouseUp);
            this._sliderBtn.stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.sliderMouseMove);
            Sprite(this._sliderBtn).stopDrag();
        }
        private function sliderSkinMouseDown(_arg1:MouseEvent):void{
            var _local2:Point = new Point(_arg1.stageX, _arg1.stageY);
            var _local3:Point = this.globalToLocal(_local2);
            this._sliderBtn.x = _local3.x;
            this.onChange();
        }
        private function sliderMouseMove(_arg1:MouseEvent):void{
            this.onChange();
        }
        private function onChange():void{
            var _local3:UIEvent;
            this.currentValue = ((this._sliderBtn.x / this._sliderBg.width) * this.max);
            var _local1:Number = this.getProgress();
            this._sliderSkin.scaleX = _local1;
            var _local2:Number = Math.round(this.currentValue);
            if (this.oldNum != _local2){
                _local3 = new UIEvent(UIEvent.CHANGE, false, false, (_local2 - this.oldNum));
                this.dispatchEvent(_local3);
                this.oldNum = _local2;
            };
        }
        public function disposeView():void{
        }
        private function getProgress():Number{
            return (Number((this.currentValue / this.max).toFixed(2)));
        }
        override public function updateUI():void{
            super.updateUI();
            var _local1:Number = this.getProgress();
            this._sliderSkin.scaleX = _local1;
            this._sliderBtn.y = ((this._sliderBtn.height - this._sliderBg.height) / 2);
            this._sliderBtn.x = (this._sliderBg.width * _local1);
        }
        override public function set width(_arg1:Number):void{
            super.width = _arg1;
            this.updateUI();
        }
        override public function set height(_arg1:Number):void{
            super.height = _arg1;
            this.updateUI();
        }
        public function get max():int{
            return (this._max);
        }
        public function set max(_arg1:int):void{
            this._max = _arg1;
        }
        public function get min():int{
            return (this._min);
        }
        public function set min(_arg1:int):void{
            this._min = _arg1;
        }
        public function get currentValue():Number{
            return (this._currentValue);
        }
        public function set currentValue(_arg1:Number):void{
            if (_arg1 > this.max){
                _arg1 = this.max;
            };
            if (_arg1 < this.min){
                _arg1 = this.min;
            };
            this._currentValue = _arg1;
        }
        public function changeValue(_arg1:Number):void{
            this.currentValue = (this.currentValue + _arg1);
            this.oldNum = this.currentValue;
            this.updateUI();
        }
        override public function set mouseEnabled(_arg1:Boolean):void{
            super.mouseEnabled = _arg1;
            super.mouseChildren = _arg1;
            if (_arg1){
                this.filters = [];
            } else {
                this.filters = [new ColorMatrixFilter([0.3, 0.6, 0, 0, 0, 0.3, 0.6, 0, 0, 0, 0.3, 0.6, 0, 0, 0, 0, 0, 0, 1, 0])];
            };
        }

    }
}//package com.fminutes.vinciUI.controls 
